﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DrugStore.Models;
using DrugStore.Exceptions;

namespace DrugStore.Controllers
{
    public class AdminController : Controller
    {               

        [MyAuthorize(LoginPage = "/Admin/LogOn", Roles = "Admin")]
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult LogOn(string returnUrl)
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            Authentication auth = new Authentication();
            if (ModelState.IsValid)
            {
                try
                {
                    auth.AdminLogOn(model);

                    AccountBusiness business = new AccountBusiness();  
             
                    if (!String.IsNullOrEmpty(returnUrl) && !returnUrl.ToLower().StartsWith("/admin"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "ManageDisease");                        
                    }                                                        
                }
                catch (ParameterInvalidException pe)
                {
                    ModelState.AddModelError(pe.ParameterName, pe.Message);
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        public ActionResult LogOff()
        {
            //FormsService.SignOut();
            Authentication auth = new Authentication();
            auth.LogOut(User.Identity.Name);
            return Redirect("/Admin/");
        }



    }
}
